﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace LibYanu.DAO
{
    [Serializable]
    class CleanUpRegistrar
    {
        private CleanUpRegistrar()
        {
            var ctx = HttpContext.Current;
            if (ctx != null)
            {
                ctx.Items[REGISTRAR_KEY] = this;
            }
        }

        internal const string REGISTRAR_KEY = "nhibernate.session.registrar";
        public event EventHandler CleanUp;
        public static CleanUpRegistrar Instance
        {
            get
            {
                var ctx = HttpContext.Current;
                if (ctx != null)
                {
                    if (ctx.Items.Contains(REGISTRAR_KEY))
                        return ctx.Items[REGISTRAR_KEY] as CleanUpRegistrar;
                    else
                        return new CleanUpRegistrar();
                }
                return null;
            }
        }

        public void Clean()
        {
            if (CleanUp != null)
                CleanUp(this, EventArgs.Empty);
            var ctx = HttpContext.Current;
            if (ctx != null)
                ctx.Items.Remove(REGISTRAR_KEY);
        }
    }
}
